
INCLUDE_SYS = ../include/sys/
KERNEL = ../kernel/
LIBC = ../lib/libc/

DIR_OBJ = $(M_DIR_OBJ)/src/api

APIDEFS= $(basename $(wildcard api_*.def))

HEADERDEFS=$(addprefix $(INCLUDE_SYS)/, $(addsuffix .h,$(APIDEFS)))
HEADERDEFS_=$(addprefix $(DIR_OBJ)/., $(addsuffix .h,$(APIDEFS)))

LIBDEFS=$(addprefix $(LIBC)/, $(addsuffix .c,$(APIDEFS)))
LIBDEFS_=$(addprefix $(DIR_OBJ)/., $(addsuffix .c,$(APIDEFS)))

KHEADERDEFS=$(addprefix $(KERNEL)/kernelApi_, $(addsuffix .h,$(APIDEFS)))
KHEADERDEFS_=$(addprefix $(DIR_OBJ)/.kernelApi_, $(addsuffix .h,$(APIDEFS)))

DOCSTXT=$(addprefix $(M_DIR_BUILD)/docs/kernel_, $(addsuffix .txt,$(APIDEFS)))
DOCSHTML=$(addprefix $(M_DIR_BUILD)/docs/kernel_, $(addsuffix .html,$(APIDEFS)))

all: \
	$(INCLUDE_SYS)/api_index.h \
	$(KERNEL)/kernelApiIndex.h

all: $(HEADERDEFS)  $(HEADERDEFS_)
all: $(LIBDEFS)     $(LIBDEFS_)
all: $(KHEADERDEFS) $(KHEADERDEFS_)
all: $(DOCSTXT)     $(DOCSHTML)

$(DIR_OBJ)/.%.h: %.def api_def.tcl Makefile
	$(Q)echo "APIDEFH "$(subst $(M_DIR_ROOT)/,,$@)
	$(Q)mkdir -p $(dir $@)
	$(Q)tclsh api_def.tcl --header < $< > $@

$(DIR_OBJ)/.%.c: %.def api_def.tcl Makefile
	$(Q)echo "APIDEFH "$(subst $(M_DIR_ROOT)/,,$@)
	$(Q)mkdir -p $(dir $@)
	$(Q)tclsh api_def.tcl --userland < $< > $@

$(DIR_OBJ)/.kernelApi_%.h: %.def api_def.tcl Makefile
	$(Q)echo "APIDEFK "$(subst $(M_DIR_ROOT)/,,$@)
	$(Q)mkdir -p $(dir $@)
	$(Q)tclsh api_def.tcl --kernel < $< > $@

$(DIR_OBJ)/.api_index.h: api.index api_index.tcl Makefile
	$(Q)echo "INDEXH  "$(subst $(M_DIR_ROOT)/,,$@)
	$(Q)mkdir -p $(dir $@)
	$(Q)tclsh api_index.tcl --userland < $< > $@

$(DIR_OBJ)/.kernelApiIndex.h: api.index api_index.tcl Makefile
	$(Q)echo "INDEXK  "$(subst $(M_DIR_ROOT)/,,$@)
	$(Q)mkdir -p $(dir $@)
	$(Q)tclsh api_index.tcl --kernel < $< > $@

$(INCLUDE_SYS)/%.h: $(DIR_OBJ)/.%.h Makefile
	$(Q)if (test -e $@ && diff -qabB $< $@) ; then : ; else cp $< $@ ; fi

$(LIBC)/%.c: $(DIR_OBJ)/.%.c Makefile
	$(Q)if (test -e $@ && diff -qabB $< $@) ; then : ; else cp $< $@ ; fi

$(KERNEL)/kernelApi_%.h: $(DIR_OBJ)/.kernelApi_%.h Makefile
	$(Q)if (test -e $@ && diff -qabB $< $@) ; then : ; else cp $< $@ ; fi

$(INCLUDE_SYS)/api_index.h: $(DIR_OBJ)/.api_index.h Makefile
	$(Q)if (test -e $@ && diff -qabB $< $@) ; then : ; else cp $< $@ ; fi

$(KERNEL)/kernelApiIndex.h: $(DIR_OBJ)/.kernelApiIndex.h Makefile
	$(Q)if (test -e $@ && diff -qabB $< $@) ; then : ; else cp $< $@ ; fi

$(M_DIR_BUILD)/docs/kernel_%.html: %.def $(M_DIR_UTILS)/api2html.sh Makefile
	$(Q)echo "DOCSTXT "$(subst $(M_DIR_ROOT)/,,$@)
	$(Q)mkdir -p $(dir $@)
	$(Q)$(M_DIR_UTILS)/api2html.sh $< > $@

$(M_DIR_BUILD)/docs/kernel_%.txt: %.def $(M_DIR_UTILS)/api2txt.sh Makefile
	$(Q)echo "DOCSHTM "$(subst $(M_DIR_ROOT)/,,$@)
	$(Q)mkdir -p $(dir $@)
	$(Q)$(M_DIR_UTILS)/api2txt.sh $< > $@

clean:
	rm -Rf *~ *.o core
